package com.starsea.book.ui.newpage.activity

import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.starsea.book.R
import com.starsea.book.base.VMBaseActivity
import com.starsea.book.databinding.ActivityAccountManageBinding
import com.starsea.book.ui.newpage.LoginActivity
import com.starsea.book.ui.newpage.adapter.AccountManageAdapter
import com.starsea.book.ui.newpage.viewmodel.WalletViewModel
import com.starsea.book.utils.startActivity
import com.starsea.book.utils.toastOnUi
import com.starsea.book.utils.viewbindingdelegate.viewBinding

/**
 * @author Lee
 * @createTime 2024-03-28 18 GMT+8
 * @desc :
 */
class AccountManageActivity : VMBaseActivity<ActivityAccountManageBinding, WalletViewModel>() {
    override val binding by viewBinding(ActivityAccountManageBinding::inflate)
    override val viewModel by viewModels<WalletViewModel>()

    lateinit var mAdapter: AccountManageAdapter


    override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
        menuInflater.inflate(R.menu.account, menu)
        return super.onCompatCreateOptionsMenu(menu)
    }


    override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
        when (item.itemId) {
            R.id.menu_share_it -> {
                startActivity<AccountAddEditActivity> {
                    putExtra("type", 0)
                }
            }

        }
        return super.onCompatOptionsItemSelected(item)
    }


    override
    fun onActivityCreated(savedInstanceState: Bundle?) {

        binding.rvCash.layoutManager = LinearLayoutManager(this)
        mAdapter = AccountManageAdapter(this) {
            startActivity<AccountAddEditActivity> {
                putExtra("type", 1)
                putExtra("accountId", it.accountId)
                putExtra("account", it.accountNumber)
                putExtra("name", it.realName)
                putExtra("isDefault", it.isDefault)
            }
        }
        binding.rvCash.adapter = mAdapter

        viewModel.accountListResult.observe(this) { data ->
            if (data.status == 200) {
                mAdapter.setItems(data.data)
            } else if (data.status == 401) {
                toastOnUi("登录过期,请重新登录")
                LoginActivity.start(this)
            } else {
                toastOnUi(data.msg)
            }
        }


    }

    override fun onResume() {
        super.onResume()
        viewModel.getAccountList()
    }
}